package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.school.entity.SchoolGradeAttendance;


public class SchoolGradeAttendanceRowMapper implements RowMapper<SchoolGradeAttendance>{
    private int type;

    
    public SchoolGradeAttendanceRowMapper() {
    }

    public SchoolGradeAttendanceRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SchoolGradeAttendance mapRow(ResultSet rs, int rowNum) throws SQLException {
        
        return parseSchoolGradeAttendance(new SchoolGradeAttendance(), rs);
    }

    public static SchoolGradeAttendance parseSchoolGradeAttendance(SchoolGradeAttendance s, ResultSet rs) throws SQLException {
        return parseSchoolGradeAttendance(s, rs, null);
    }

    public static SchoolGradeAttendance parseSchoolGradeAttendance(SchoolGradeAttendance s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setSgid(rs.getLong(su+"sgid"));
        } catch (SQLException e) {
            s.setSgid(null);
        }
        try {
            s.setGid(rs.getLong(su+"gid"));
        } catch (SQLException e) {
            s.setGid(null);
        }
        try {
            s.setType(rs.getShort(su+"type"));
        } catch (SQLException e) {
            s.setType(null);
        }
        try {
            s.setSadate(rs.getTime(su+"sadate"));
        } catch (SQLException e) {
            s.setSadate(null);
        }
        try {
            s.setEadate(rs.getTime(su+"eadate"));
        } catch (SQLException e) {
            s.setEadate(null);
        }
        try {
            s.setSpdate(rs.getTime(su+"spdate"));
        } catch (SQLException e) {
            s.setSpdate(null);
        }
        try {
            s.setEpdate(rs.getTime(su+"epdate"));
        } catch (SQLException e) {
            s.setEpdate(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SchoolGradeAttendance s) {
        par.put("gid", s.getGid());
        par.put("sgid",s.getSgid());
        par.put("type", s.getType());
        par.put("sadate", s.getSadate());
        par.put("eadate", s.getEadate());
        par.put("spdate", s.getSpdate());
        par.put("epdate", s.getEpdate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SchoolGradeAttendance s) {
        args.put("sgid",s.getSgid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SchoolGradeAttendance s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "sgid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(SchoolGradeAttendance s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into school_grade_attendance(");
        int count = 0;
        if (null != s.getGid()) {
            sql.append("gid,");
            count++;
        }

        if (null !=s.getType()) {
            sql.append("type,");
            count++;
        }
        if (null !=s.getSadate()) {
            sql.append("sadate,");
            count++;
        }
        
        if (null !=s.getEadate()) {
            sql.append("eadate,");
            count++;
        }
        
        if (null !=s.getSpdate()) {
            sql.append("spdate,");
            count++;
        }
        
        if (null !=s.getEpdate()) {
            sql.append("epdate,");
            count++;
        }
        

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SchoolGradeAttendance s)
        throws SQLException {
        int count = 1;
        if (null != s.getGid()) {
            ps.setLong(count++,s.getGid());
        }
        if (null != s.getType()) {
            ps.setInt(count++, s.getType());
        }
        if (null != s.getSadate()) {
            ps.setTime(count++, s.getSadate());
        }
        if (null != s.getEadate()) {
            ps.setTime(count++, s.getEadate());
        }
        if (null != s.getSpdate()) {
            ps.setTime(count++, s.getSpdate());
        }
        if (null != s.getEpdate()) {
            ps.setTime(count++, s.getEpdate());
        }
        


    }

}
